package com.qys.livesMall.shop.controller.sys;

import com.qys.livesMall.common.exception.QysException;
import com.qys.livesMall.common.result.R;
import com.qys.livesMall.finance.enums.AccountBusinessTypeEnum;
import com.qys.livesMall.finance.model.dto.AccountSearch;
import com.qys.livesMall.finance.model.dto.FinancePayDTO;
import com.qys.livesMall.finance.service.FinanceBusinessService;
import com.qys.livesMall.finance.util.BusinessNoUtil;
import com.qys.livesMall.framework.view.QysPage;
import com.qys.livesMall.shop.dao.H2ShopBaseDao;
import com.qys.livesMall.shop.enums.ShopBusinessTypeEnum;
import com.qys.livesMall.shop.enums.ShopMessageEnum;
import com.qys.livesMall.shop.modal.dto.shop.balance.ShopBalanceHandleDTO;
import com.qys.livesMall.shop.modal.entity.H2ShopBase;
import com.qys.livesMall.shop.modal.vo.ShopBalanceVO;
import com.qys.livesMall.shop.service.H2ShopBalanceService;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;

/**
 * 后台管理-店铺余额表
 *
 * @author
 * @since 2023-06-06
 */
@RestController
@RequestMapping("/shop/balance")
public class WebH2ShopBalanceController {

    @Autowired
    private H2ShopBalanceService shopBalanceService;
    @Autowired
    private H2ShopBaseDao shopBaseDao;
    @Autowired
    private FinanceBusinessService financeBusinessService;

    /**
     * 列表
     * @param search
     * @return
     */
    @PostMapping("list")
    public R<QysPage<ShopBalanceVO>> list(@RequestBody AccountSearch search){
        return R.ok(shopBalanceService.list(search));
    }

    @PostMapping("/trading")
    @Schema(description = "店铺交易（赠送、扣款）")
    public R<?> trading(@RequestBody ShopBalanceHandleDTO dto){

        H2ShopBase shopBase = shopBaseDao.getById(dto.getShopId());
        if(null == shopBase){
            throw new QysException(ShopMessageEnum.SHOP_NOT_EXIST);
        }

        FinancePayDTO financePayDTO = new FinancePayDTO();
        financePayDTO.setUserId(shopBase.getUserId());
        financePayDTO.setShopId(Long.parseLong(dto.getShopId()));
        financePayDTO.setAmountIncurred(new BigDecimal(dto.getAmountIncurred()));
        financePayDTO.setDescription(dto.getDescription());

        if(ShopBusinessTypeEnum.DEDUCT.getType().equals(dto.getCategory())){
            //扣款
            financePayDTO.setBusinessNo(BusinessNoUtil.get(AccountBusinessTypeEnum.SHOP_DEDUCT));
            financePayDTO.setBusinessType(AccountBusinessTypeEnum.SHOP_DEDUCT);
        }else if(ShopBusinessTypeEnum.GIFT.getType().equals(dto.getCategory())){
            //赠送
            financePayDTO.setBusinessNo(BusinessNoUtil.get(AccountBusinessTypeEnum.SHOP_GIFT));
            financePayDTO.setBusinessType(AccountBusinessTypeEnum.SHOP_GIFT);
        }
        financeBusinessService.pay(financePayDTO);

        if(!financePayDTO.getSuccess()){
            throw new QysException(financePayDTO.getMessage());
        }

        return R.ok();
    }

}
